﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DelonixRegiaHMSys.Infrastructure.Data;
using DelonixRegiaHMSys.Core.Services;
using DelonixRegiaHMSys.Core.Models;

namespace DelonixRegiaHMSys.UI.WebForms
{
    public partial class RoomSelection : System.Web.UI.Page
    {
        string userID;
        string checkinID;
        string guestID;
        string reservationID;
        string numOfA;
        string numOfC;
        string checkinDateTime;
        string desiredCheckOutDateTime;
        string rT;
        string bT;
        string sON;
        string lateCheckOutReq;
        string pM;
        string aR;
        protected void Page_Load(object sender, EventArgs e)
        {
            userID = Request.QueryString["UserID"];
            checkinID = Request.QueryString["CheckInID"];
            guestID = Request.QueryString["GuestID"];
            reservationID = Request.QueryString["ReservationID"];
            numOfA = Request.QueryString["NumOfAdults"];
            numOfC = Request.QueryString["NumOfChildren"];
            checkinDateTime = Request.QueryString["CheckInDateTime"];
            desiredCheckOutDateTime = Request.QueryString["DesiredCheckOutDateTime"];
            rT = Request.QueryString["RoomType"];
            bT = Request.QueryString["BedType"];
            sON = Request.QueryString["SmokingOrNot"];
            lateCheckOutReq = Request.QueryString["LateCheckOutReq"];
            pM = Request.QueryString["PaymentMode"];
            aR = Request.QueryString["AddRemarks"];
            if (!IsPostBack)
            {
                BindGridView();
            }
        }

        private void BindGridView()
        {
            var roservice = new roServices(new EfRoomQuery());
            DataListRoomAvail.DataSource = roservice.GetRoomByGuestRequirements(rT, bT, sON).ToList();
            DataListRoomAvail.DataBind();                                
        }

        protected void lrbtn_Click(object sender, EventArgs e)
        {
            var ciservice = new ciServices(new EfCheckInQuery());
            var roservice = new roServices(new EfRoomQuery());
            CheckIn ci = new CheckIn();
            ci.CheckInID = checkinID;
            ci.GuestID = guestID;
            ci.ReservationID = reservationID;
            ci.UserID = userID;
            ci.NumOfAdults = Convert.ToInt32(numOfA);
            ci.NumOfChildren = Convert.ToInt32(numOfC);
            ci.CheckInDateTime = Convert.ToDateTime(checkinDateTime);
            ci.DesiredCheckOutDateTime = Convert.ToDateTime(desiredCheckOutDateTime);
            ci.RoomType = rT;
            ci.BedType = bT;
            ci.SmokingOrNot = sON;
            ci.LateCheckOutRequest = lateCheckOutReq;
            ci.PaymentMode = pM;
            ci.AddRemarks = aR;
            ci.RoomNum = Convert.ToInt32((((LinkButton)sender).CommandArgument));
            ci.Status = "SCI";

            ciservice.AddCheckIn(ci);
            bool SuccessOrFail = ciservice.CheckInRecordAdded(ci.CheckInID);
            if (SuccessOrFail == true)
            {
                roservice.UpdateRoom(ci.RoomNum, "O", ci.GuestID);
                string queryString = "UserID=" + userID + "&CheckInID=" + ci.CheckInID;
                Server.Transfer("~/WebForms/RRandCIModule/AddRecordSuccess.aspx?" + queryString);
            }
            else
            {

            }
        }

    }
}